package com.study.serializable;

import java.io.*;

public class test1 {
    public static void main(String[] args) throws IOException {
//        序列化
      try {
          FileOutputStream fileOutputStream = new FileOutputStream("./user1.txt");
          ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutputStream);

          User1 user1 = new User1(1,"杨桐",12);
          objectOutput.writeObject(user1);
          objectOutput.close();
          fileOutputStream.close();
          System.out.println("serialization  success");
      }catch (Exception e){
          System.out.println(e);
      }

        //反序列化
//        FileInputStream fileInputStream = new FileInputStream("./user1.txt");
//        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//        try {
//            User1 user2 = (User1) objectInputStream.readObject();
//            System.out.println(user2);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }

    }
}

class User1 implements Serializable{
    private static final long serialVersionUID = -3755237233382726947L;
    private int id;
    private String name;
    private transient int age;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User1(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
